﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using NUnit.Framework;
using Air_Trafic_Control.UI;
using Air_Trafic_Control.Domain.Control;
using Air_Trafic_Control.Domain.Persistence;
using Air_Trafic_Control.Domain.Common;
using Air_Trafic_Control.Domain.Entity;
using Air_Traffic_Control_Test.Domain;
using System.Threading;

namespace Air_Traffic_Control_Test
{
    [SetUpFixture]
    public class Program
    {
        public static ControlConsole console;

        /// <summary>
        /// The main entry point for the application. This will create a new ControlConsole, 
        /// create the EAT, set the selected user, and set the selected game.
        /// </summary>
        [STAThread]
        static void Main()
        {
            
        }

        [SetUp]
        public void SetUp()
        {
            // Declare our variables
            EAT m_eat;
            UserPersistenceHandler uph = null;
            GamePersistenceHandler gph = null;
            User unitTestUser = null;
            Game unitTestGame = null;
            string temp = "";
            uint i = 0;

            // Get the 1 and only EAT
            m_eat = EAT.GetEAT();

            // Get the persistence handlers
            uph = new UserPersistenceHandler(m_eat.PersistenceDirectory);

            // Ensure there is a UnitTest user
            foreach (User u in uph.Users)
            {
                if (u.Name.Equals(ConstantsTest.UNIT_TEST_USER_NAME))
                {
                    unitTestUser = u;
                    break;
                }
            }

            if (unitTestUser == null)
            {
                unitTestUser = m_eat.CreateNewUser(ConstantsTest.UNIT_TEST_USER_NAME);
            }

            // Set the selected user
            m_eat.SelectedUser = unitTestUser;

            // Create a GamePersistenceHandler for the user
            gph = new GamePersistenceHandler(m_eat.PersistenceDirectory, unitTestUser.Name);

            // Display
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Program.console = new ControlConsole();
            (new Thread(new ThreadStart(StartApp))).Start();

            // Create a new Game
            for (i = 0; i < 10000; i++)
            {
                bool matched = false;

                temp = "UnitTestGame" + i.ToString();

                foreach (Game g in gph.Games)
                {
                    if (g.Name.Equals(temp))
                    {
                        matched = true;
                        break;
                    }
                }

                // Was this game name already taken?
                if (matched && i < 9999)
                {
                    continue;
                }
                // Have we exhausted all game names?
                else if (matched && i == 9999)
                {
                    System.Console.WriteLine("Tried 10000 names for a new game but all were taken. Please delete the old games and try again ;-) Thanks!");
                    Application.Exit();
                }
                // Did we find a game name we can use?
                else
                {
                    unitTestGame = new Game(temp);
                    m_eat.CreateNewGame(unitTestUser, unitTestGame);
                    break;
                }
            }

            // Set the selected Game
            m_eat.SelectedGame = unitTestGame;
        }

        public void StartApp()
        {
            Application.Run(Program.console);
        }

        [TearDown]
        public void TearDown()
        {
            // Hide
            Program.console.Hide();
            Program.console.Dispose();
        }
    }
}
